import { useStore } from '@/hooks';
import { Dialog, Message } from '@alifd/next';
import { t } from '@lingui/macro';
import { observer } from 'mobx-react-lite';
import React from 'react';
import { MAGIC_NUMBERS } from '../stores/definitions';

const FinishTaskDialog = (props) => {
  const { visible, onClose, task, action } = props;
  const { DRIVER_STATE } = MAGIC_NUMBERS;
  const { ccTaskStore: store } = useStore();

  const finishTask = async () => {
    try {
      const { id: taskId } = task;
      const changeStateMap = {
        pickUpPassenger: DRIVER_STATE.GOING,
        pickedPassenger: DRIVER_STATE.PICKED,
        arrive: DRIVER_STATE.FINISH,
        finishTask: DRIVER_STATE.FINISH,
      };
      await store.changeTaskState(taskId, changeStateMap[action]);
      Message.success({ content: t`状态修改成功` });
    } catch (e) {
      Message.error({ content: t`状态修改失败!${e.source?.message}`, duration: 7000 });
      throw e;
    }
    onClose?.();
  };

  return (
    <Dialog
      visible={visible}
      v2
      onClose={onClose}
      onOk={finishTask}
    >
      <p>{t`确定更改任务状态吗?`}</p>
    </Dialog>
  );
};

export default observer(FinishTaskDialog);
